package com.joysuch.wwyt.risk.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.risk.entity.RiskMeasureType;
import com.joysuch.wwyt.risk.entity.vo.RiskMeasureTypeNode;
import com.joysuch.wwyt.risk.service.RiskMeasureTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author heqifeng
 * @since 2021-09-10
 */
@RestController
@Api(tags = "双预防-管控措施类型")
@RequestMapping("/risk/risk-measure-type")
public class RiskMeasureTypeController {

    @Autowired
    RiskMeasureTypeService service;


    @ApiOperation("获取所有顶级分类")
    @GetMapping("getAllTopTypes")
    public ResultBean getTopTypes() {
        return ResultBean.success(service.getAllTopTypes());
    }

    @ApiOperation("获取所有下级分类")
    @GetMapping("getAllChilds")
    public ResultBean getAllChilds(Long id) {
        return ResultBean.success(service.getAllChilds(id));
    }

    @ApiOperation("配置父类")
    @RequestMapping("addTopTypes")
    public ResultBean addTopTypes(@RequestBody List<RiskMeasureType> measureType) {
        int addCount = service.addTopTypes(measureType);
        if (addCount == -1) {
            return ResultBean.fail(Status.DUPLICATE_NAME);
        }
        return ResultBean.defaultSuccessResult();
    }

    @ApiOperation("配置子类")
    @RequestMapping("addChildTypes")
    public ResultBean addChildTypes(@RequestBody List<RiskMeasureTypeNode> riskMeasureTypeNodes) {
        int saved = service.addChildTypes(riskMeasureTypeNodes);
        if (saved == -1) {
            return ResultBean.fail(Status.DUPLICATE_NAME);
        }
        return ResultBean.success(saved);
    }

    @ApiOperation("获取父分类至顶级分类")
    @RequestMapping("getParents")
    public ResultBean getParents(Long id) {
        return ResultBean.success(service.findAllParentsByChildId(id));
    }
}

